{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to download the public companies' earnings calendar in Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "from datetime import timedelta\n",
    "from yahoo_earnings_calendar import YahooEarningsCalendar\n",
    "import dateutil.parser"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Downloading the earnings calendar using Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For a specific date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ticker</th>\n",
       "      <th>companyshortname</th>\n",
       "      <th>startdatetime</th>\n",
       "      <th>startdatetimetype</th>\n",
       "      <th>epsestimate</th>\n",
       "      <th>epsactual</th>\n",
       "      <th>epssurprisepct</th>\n",
       "      <th>gmtOffsetMilliSeconds</th>\n",
       "      <th>quoteType</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ASFI</td>\n",
       "      <td>Asta Funding Inc</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ABCE</td>\n",
       "      <td>ABCO Energy Inc</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ATHM</td>\n",
       "      <td>Autohome Inc</td>\n",
       "      <td>2020-05-15T16:30:00.000Z</td>\n",
       "      <td>BMO</td>\n",
       "      <td>0.76</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>UAVS</td>\n",
       "      <td>Ageagle Aerial Systems Inc</td>\n",
       "      <td>2020-05-16T00:00:00.000Z</td>\n",
       "      <td>AMC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AMCI</td>\n",
       "      <td>AMCI Acquisition Corp</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  ticker            companyshortname             startdatetime  \\\n",
       "0   ASFI            Asta Funding Inc  2020-05-15T14:59:00.000Z   \n",
       "1   ABCE             ABCO Energy Inc  2020-05-15T14:59:00.000Z   \n",
       "2   ATHM                Autohome Inc  2020-05-15T16:30:00.000Z   \n",
       "3   UAVS  Ageagle Aerial Systems Inc  2020-05-16T00:00:00.000Z   \n",
       "4   AMCI       AMCI Acquisition Corp  2020-05-15T14:59:00.000Z   \n",
       "\n",
       "  startdatetimetype  epsestimate  epsactual  epssurprisepct  \\\n",
       "0               TNS          NaN        NaN             NaN   \n",
       "1               TNS          NaN        NaN             NaN   \n",
       "2               BMO         0.76        NaN             NaN   \n",
       "3               AMC          NaN        NaN             NaN   \n",
       "4               TNS          NaN        NaN             NaN   \n",
       "\n",
       "   gmtOffsetMilliSeconds quoteType  \n",
       "0                      0    EQUITY  \n",
       "1                      0    EQUITY  \n",
       "2                      0    EQUITY  \n",
       "3                      0    EQUITY  \n",
       "4                      0    EQUITY  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# setting the report date\n",
    "report_date = datetime.now().date()\n",
    "\n",
    "# downloading the earnings calendar\n",
    "yec = YahooEarningsCalendar()\n",
    "earnings_list = yec.earnings_on(report_date)\n",
    "\n",
    "# saving the data in a pandas DataFrame\n",
    "earnings_df = pd.DataFrame(earnings_list)\n",
    "earnings_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(119, 9)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "earnings_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'ticker': 'ASFI',\n",
       "  'companyshortname': 'Asta Funding Inc',\n",
       "  'startdatetime': '2020-05-15T14:59:00.000Z',\n",
       "  'startdatetimetype': 'TNS',\n",
       "  'epsestimate': None,\n",
       "  'epsactual': None,\n",
       "  'epssurprisepct': None,\n",
       "  'gmtOffsetMilliSeconds': 0,\n",
       "  'quoteType': 'EQUITY'},\n",
       " {'ticker': 'ABCE',\n",
       "  'companyshortname': 'ABCO Energy Inc',\n",
       "  'startdatetime': '2020-05-15T14:59:00.000Z',\n",
       "  'startdatetimetype': 'TNS',\n",
       "  'epsestimate': None,\n",
       "  'epsactual': None,\n",
       "  'epssurprisepct': None,\n",
       "  'gmtOffsetMilliSeconds': 0,\n",
       "  'quoteType': 'EQUITY'}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "earnings_list[:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For a range of dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ticker</th>\n",
       "      <th>companyshortname</th>\n",
       "      <th>startdatetime</th>\n",
       "      <th>startdatetimetype</th>\n",
       "      <th>epsestimate</th>\n",
       "      <th>epsactual</th>\n",
       "      <th>epssurprisepct</th>\n",
       "      <th>gmtOffsetMilliSeconds</th>\n",
       "      <th>quoteType</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AMPG</td>\n",
       "      <td>Amplitech Group Inc</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ABCE</td>\n",
       "      <td>ABCO Energy Inc</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ASFI</td>\n",
       "      <td>Asta Funding Inc</td>\n",
       "      <td>2020-05-15T14:59:00.000Z</td>\n",
       "      <td>TNS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ATHM</td>\n",
       "      <td>Autohome Inc</td>\n",
       "      <td>2020-05-15T16:30:00.000Z</td>\n",
       "      <td>BMO</td>\n",
       "      <td>0.76</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>UAVS</td>\n",
       "      <td>Ageagle Aerial Systems Inc</td>\n",
       "      <td>2020-05-16T00:00:00.000Z</td>\n",
       "      <td>AMC</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  ticker            companyshortname             startdatetime  \\\n",
       "0   AMPG         Amplitech Group Inc  2020-05-15T14:59:00.000Z   \n",
       "1   ABCE             ABCO Energy Inc  2020-05-15T14:59:00.000Z   \n",
       "2   ASFI            Asta Funding Inc  2020-05-15T14:59:00.000Z   \n",
       "3   ATHM                Autohome Inc  2020-05-15T16:30:00.000Z   \n",
       "4   UAVS  Ageagle Aerial Systems Inc  2020-05-16T00:00:00.000Z   \n",
       "\n",
       "  startdatetimetype  epsestimate  epsactual  epssurprisepct  \\\n",
       "0               TNS          NaN        NaN             NaN   \n",
       "1               TNS          NaN        NaN             NaN   \n",
       "2               TNS          NaN        NaN             NaN   \n",
       "3               BMO         0.76        NaN             NaN   \n",
       "4               AMC          NaN        NaN             NaN   \n",
       "\n",
       "   gmtOffsetMilliSeconds quoteType  \n",
       "0                      0    EQUITY  \n",
       "1                      0    EQUITY  \n",
       "2                      0    EQUITY  \n",
       "3                      0    EQUITY  \n",
       "4                      0    EQUITY  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DAYS_AHEAD = 7\n",
    "\n",
    "# setting the dates\n",
    "start_date = datetime.now().date()\n",
    "end_date = (datetime.now().date() + timedelta(days=DAYS_AHEAD))\n",
    "\n",
    "# downloading the earnings calendar\n",
    "yec = YahooEarningsCalendar()\n",
    "earnings_list = yec.earnings_between(start_date, end_date)\n",
    "\n",
    "# saving the data in a pandas DataFrame\n",
    "earnings_df = pd.DataFrame(earnings_list)\n",
    "earnings_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(312, 9)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "earnings_df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### For a specific product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ticker</th>\n",
       "      <th>companyshortname</th>\n",
       "      <th>startdatetime</th>\n",
       "      <th>startdatetimetype</th>\n",
       "      <th>epsestimate</th>\n",
       "      <th>epsactual</th>\n",
       "      <th>epssurprisepct</th>\n",
       "      <th>gmtOffsetMilliSeconds</th>\n",
       "      <th>quoteType</th>\n",
       "      <th>report_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>TWTR</td>\n",
       "      <td>Twitter Inc</td>\n",
       "      <td>2020-07-24T16:30:00.000Z</td>\n",
       "      <td>BMO</td>\n",
       "      <td>0.02</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "      <td>2020-07-24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>TWTR</td>\n",
       "      <td>Twitter Inc</td>\n",
       "      <td>2020-10-22T16:30:00.000Z</td>\n",
       "      <td>BMO</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>EQUITY</td>\n",
       "      <td>2020-10-22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  ticker companyshortname             startdatetime startdatetimetype  \\\n",
       "3   TWTR      Twitter Inc  2020-07-24T16:30:00.000Z               BMO   \n",
       "2   TWTR      Twitter Inc  2020-10-22T16:30:00.000Z               BMO   \n",
       "\n",
       "   epsestimate  epsactual  epssurprisepct  gmtOffsetMilliSeconds quoteType  \\\n",
       "3         0.02        NaN             NaN                      0    EQUITY   \n",
       "2          NaN        NaN             NaN                      0    EQUITY   \n",
       "\n",
       "  report_date  \n",
       "3  2020-07-24  \n",
       "2  2020-10-22  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# setting the parameters \n",
    "TICKER = 'TWTR'\n",
    "DAYS_AHEAD = 180\n",
    "\n",
    "# setting the dates\n",
    "start_date = datetime.now().date()\n",
    "end_date = (datetime.now().date() + timedelta(days=DAYS_AHEAD))\n",
    "\n",
    "# downloading the earnings calendar\n",
    "yec = YahooEarningsCalendar()\n",
    "earnings_list = yec.get_earnings_of(TICKER)\n",
    "earnings_df = pd.DataFrame(earnings_list)\n",
    "\n",
    "# extracting the date from the string and filtering for the period of interest\n",
    "earnings_df['report_date'] = earnings_df['startdatetime'].apply(lambda x: dateutil.parser.isoparse(x).date())\n",
    "earnings_df = earnings_df.loc[earnings_df['report_date'].between(start_date, end_date)] \\\n",
    "                         .sort_values('report_date')\n",
    "\n",
    "earnings_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
